package com.company.cloud.ad.modules.mgr.controller;

import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.ad.modules.base.entity.AdPositionInfo;
import com.company.cloud.ad.modules.base.service.AdPositionInfoService;
import com.company.cloud.ad.modules.cache.models.AdPositionModel;
import com.company.cloud.ad.modules.cache.service.AdCacheService;
import com.company.cloud.ad.modules.cache.util.AdConvertUtils;
import com.company.cloud.ad.modules.mgr.controller.req.AdPositionMgrAllocationParam;
import com.company.cloud.ad.modules.mgr.controller.req.AdPositionMgrQuery4PageParam;
import com.company.cloud.ad.modules.mgr.controller.req.AdPositionMgrSaveParam;
import com.company.cloud.ad.modules.mgr.service.AdMgrService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 广告位管理
 */
@Authc
@RestController
@RequestMapping("/mgr/ad/adPosition")
@Api(tags="Mgr_Ad_广告位管理")
public class AdPositionMgrController {

	
	@Autowired
	private AdPositionInfoService adPositionInfoService;
	@Autowired
	private AdCacheService adCacheService;
	@Autowired
	private AdMgrService adMgrService;
	
	
	@PostMapping("/query")
	@ApiOperation(value="广告位信息列表查询", response= AdPositionInfo.class)
	public BaseResult query(@RequestBody AdPositionMgrQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		String inSQL=null;
		if(CheckUtils.hasInt(params.getAdId())) {
			inSQL=StrUtil.format("select ad_position_no from cms_ad_position_rel_info where ad_id={}", params.getAdId());
		}
		IPage<AdPositionInfo> page = adPositionInfoService.page(new Page<AdPositionInfo>(params.getPage(), params.getRows()),
				new QueryWrapper<AdPositionInfo>()
				.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
				.like(StringUtils.hasText(params.getName()), "name", params.getName())
				.inSql(CheckUtils.hasInt(params.getAdId()), "ad_position_no", inSQL)
				);
		
		IPage<AdPositionModel> data=page.convert(new Function<AdPositionInfo, AdPositionModel>() {
			@Override
			public AdPositionModel apply(AdPositionInfo entity) {
				return  AdConvertUtils.getAdPositionModel(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	@GetMapping("/get")
	@ApiOperation(value="广告位信息详情", response = AdPositionInfo.class)
	public BaseResult get(@RequestParam("adPositionNo")String adPositionNo) {
		AdPositionInfo entity = adPositionInfoService.getById(adPositionNo);
		if(entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		AdPositionModel data= AdConvertUtils.getAdPositionModel(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@ApiOperation(value="广告位信息新增")
	public BaseResult save(@RequestBody AdPositionMgrSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		AdPositionInfo old = adPositionInfoService.getById(params.getAdPositionNo());
		if (old == null) {
			AdPositionInfo entity=new AdPositionInfo();
			entity.setAdPositionNo(params.getAdPositionNo());
			entity.setName(params.getName());
			entity.setTemplate(params.getTemplate());
			entity.setDescription(params.getDescription());
			entity.setHeight(params.getHeight());
			entity.setWidth(params.getWidth());
			entity.addCreateParam(userInfo);
			entity.addEditParam(userInfo);
			adPositionInfoService.save(entity);
		} else {
			if (BaseConstant.EnabledEnum.ENABLED.getCode().equals(old.getRowsState())) {
				throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
			}
			old.setAdPositionNo(params.getAdPositionNo());
			old.setName(params.getName());
			old.setTemplate(params.getTemplate());
			old.setDescription(params.getDescription());
			old.setHeight(params.getHeight());
			old.setWidth(params.getWidth());
			old.setRowsState(BaseConstant.EnabledEnum.ENABLED.getCode());
			old.addEditParam(userInfo);
			adPositionInfoService.updateById(old);
		}
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@ApiOperation(value="广告位信息修改")
	public BaseResult update(@RequestBody AdPositionMgrSaveParam params) {
		ValidatorUtils.validate(params);
		AdPositionInfo entity=adPositionInfoService.getById(params.getAdPositionNo());
		if(entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setName(params.getName());
		entity.setTemplate(params.getTemplate());
		entity.setDescription(params.getDescription());
		entity.setHeight(params.getHeight());
		entity.setWidth(params.getWidth());
		entity.addEditParam(userInfo);
		adPositionInfoService.updateById(entity);
		return BaseResult.ok();
	}
	
	@GetMapping("/delete")
	@ApiOperation(value="广告位信息删除")
	public BaseResult delete(@RequestParam("adPositionNo")String adPositionNo) {
		AdPositionInfo entity=adPositionInfoService.getById(adPositionNo);
		if(entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		adPositionInfoService.updateById(entity);
		adCacheService.removeAdByCache(adPositionNo);
		return BaseResult.ok();
	}
	
	
	@GetMapping("/allocation")
	@ApiOperation(value="根据广告位分配广告")
	public BaseResult allocation(@RequestBody AdPositionMgrAllocationParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		adMgrService.allocation(params, userInfo);
		return BaseResult.ok();
	}
	
	
}
